Skip to content

macOS support: getfsstat mounts, firmlinks, Finder Trash, purgeable space - #8

Closed
alexzub wants to merge 2 commits into
tobi:mainfrom
alexzub:macos-support
Closed

alexzub wants to merge 2 commits into
tobi:mainfrom
alexzub:macos-support

Conversation

@alexzub

@alexzub alexzub commented Sep 25, 2026 •

Copy link
Copy Markdown

disktree already builds and draws on macOS through GPUI's Metal backend. What did not carry over was the Linux plumbing underneath. On macOS today:

  • "Move to trash" falls through to ~/.local/share/Trash, where the Finder never looks;
  • /proc/self/mounts does not exist, so --disk scans / with no mount table. The device fallback has to open each directory, so it can wake a mounted network share;
  • the system-tree guard has no macOS paths.

This PR fixes these in disktree-core, and it does not touch the UI crate:

  • Mounts: read with getfsstat(2) and MNT_NOWAIT, so a dead SMB/NFS share cannot stall a scan and is never entered.
  • Firmlinks: statfs(2) gives the real volume of a path, so /Users resolves to the Data volume. --disk scans / and skips /System/Volumes/Data, so the sealed system and every user file are each counted once.
  • Trash: uses NSFileManager, so items go to the Finder's Trash and Put Back works.
  • Free space: includes purgeable space, the figure that the Finder and System Settings show.
  • System trees: adds /System, /Library, /private/{etc,var/db,var/vm} and /opt/homebrew on macOS, judged through both faces of a firmlink and without case (the default APFS volume is case-insensitive).
  • Firmlink aliases for mounts: each mount also gets its second spelling from /usr/share/firmlinks, so a share at /Volumes/NAS is also left out at /System/Volumes/Data/Volumes/NAS.
  • Removal guards (all platforms): a parent of the home directory is refused. /Users is not a mount point on macOS, and /home need not be one on Linux, so without this check remove_dir_all would empty the home directory before it failed on the root-owned parent. A parent of a system tree is refused too (/var holds /var/lib). Before each removal, the worker checks the mount table for a filesystem mounted inside the target, because remove_dir_all does not stop at mount points.
  • CI: adds a macos-15 job with the same lint and test steps.

All macOS code is in disktree-core/src/macos.rs or behind cfg(target_os = "macos"). getfsstat is the one unsafe call, with a documented allow. libc and objc2 are already in the graph through GPUI, so the change adds no crates (the Cargo.lock diff is 3 lines).

Tested

  • cargo xtask lint and cargo xtask test on Linux and macOS in CI, and locally on 1.97 and 1.98.
  • 15 new core tests: the real mount table and its firmlink aliases, firmlink resolution, purgeable space, a round trip through the Finder's Trash (the test cleans up after itself), the macOS system trees without case, parents of home and of system trees, and a mount inside a target.
  • whole_disk_smoke on a 460 GB Mac: 333 GiB in 37 s. It left out 13 foreign mounts (NAS shares, simulator images, the other APFS system volumes), with no double counting.
  • By hand: marked a file, moved it to the Trash from the review screen, and confirmed it in the Finder's Trash. In a --disk scan, marked /Users: the review screen keeps it back ("it contains the home directory") and acts on 0 items.

Not in this PR

  • make install still writes a Linux desktop entry on macOS. A .app bundle and a macOS release build (DMG and tarball) can be a follow-up PR if you want them.
  • On macOS, the refusal text for system trees still says "use the package manager".

🤖 Generated with Claude Code

disktree already builds and draws on macOS through GPUI's Metal backend;
what did not carry over was the Linux plumbing underneath.

- Mount table from getfsstat(2) with MNT_NOWAIT instead of
  /proc/self/mounts, so foreign mounts (SMB/NFS shares, simulator
  images, /dev, the other APFS system volumes) are left out by path
  again and a dead network share cannot stall a scan.
- Firmlinks: the volume a path lives on comes from statfs(2), so /Users
  resolves to the Data volume. --disk scans / and skips
  /System/Volumes/Data, counting the sealed system and every user file
  once.
- Move to trash uses NSFileManager, the Finder's Trash with Put Back,
  instead of falling through to ~/.local/share/Trash.
- Free space includes purgeable space (the Finder's figure).
- System trees gain /System, /Library, /private/{etc,var/db,var/vm} and
  /opt/homebrew, judged through either face of a firmlink.
- CI runs lint and tests on macos-15 as well.

The macOS code lives in disktree-core/src/macos.rs. getfsstat is the
one unsafe call, with a documented allow; libc and objc2 are already in
the graph through GPUI, so no crates are added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alexzub

alexzub commented Sep 25, 2026 •

Copy link
Copy Markdown
Author

How this compares with #1, #3 and #7

Update (commit 4b13a63): #8 now also refuses parents of home and of system trees, checks for mounts inside a target before each removal, compares guards without case on macOS, and aliases mounts through /usr/share/firmlinks. The rows below are updated. I have also corrected an error: my first version gave /home as an example of a missed firmlink alias. /home is a symlink, and the scan does not follow symlinks, so that example was wrong. The real case is a share at /Volumes/X, which is also reachable at /System/Volumes/Data/Volumes/X.

Four macOS PRs came in within about six hours, and I should have checked before I opened this one. To save you some review time, here is a side-by-side comparison. I made it by reading all four diffs. I have not run the other branches. Note that #7 is built on #1's two commits (b6c1b11, dbca8b2), so #7 includes all of #1.

#1 #3 #7 #8 (this)
Size +269 / −9 +1581 / −147 +680 / −24 +745 / −22
Mount table unchanged (/proc/self/mounts, so none on macOS) getfsstat, MNT_NOWAIT; retries if the table grows getfsstat, MNT_NOWAIT; gives up if the table grows getfsstat, MNT_NOWAIT; +8 headroom, truncates
--disk root unchanged (falls back to /, no table) /System/Volumes/Data /System/Volumes/Data /, skipping the Data mount point (sealed system included, each file counted once)
Mounts seen through firmlinks – yes, from /usr/share/firmlinks yes, Data ↔ / spellings yes, from /usr/share/firmlinks
Trash /usr/bin/trash NSFileManager /usr/bin/trash -s NSFileManager
Free space statvfs statvfs statvfs includes purgeable space (…ForImportantUsageKey, the Finder's figure)
Protected trees – /System, /Library, /private/{etc,var/db,var/root,var/run,var/vm} /System, /Library, /private/etc, all of /private/var /System, /Library, /private/{etc,var/db,var/vm}, /opt/homebrew
Case-insensitive guard – yes yes (plus an inode check for home) yes
Refuses ancestors of home (e.g. /Users) no yes no yes
Checks for nested mounts before a delete no yes no yes
Scanner unchanged getattrlistbulk fast path unchanged unchanged
App bundle / install make app; make install → ~/Applications + symlink make bundle, committed .icns, signed build on the author's fork same as #1 none
macOS UI o = reveal in Finder menu bar, ⌘ shortcuts, Open Folder, Full Disk Access same as #1 none (UI crate untouched)
CI none none matrix ubuntu-24.04 + macos-15, plus a release build separate macos-15 job
New tests 4 12 11 15

One safety point worth a look whichever PR you take

On macOS, /Users is not a mount point, so the upstream is_mount_point guard does not catch it. In a --disk scan, #1 and #7 accept marking /Users (or any other ancestor of home), and so did the first version of #8. A permanent delete then uses remove_dir_all, which removes everything the user owns inside their home before it fails on the root-owned /Users itself. #3 refuses ancestors of home and of system trees, and a directory with a mount below it. #8 now does the same (the home-ancestor and nested-mount checks apply on Linux too).

Where #8 is weaker

What only #8 adds

If #3 or #7 is the direction you want, I'm happy to rebase the purgeable-space change onto whichever one lands, as a small follow-up.

Guards that the first macOS pass left out, found while comparing with the
other macOS PRs:

- A parent of the home directory is refused. /Users is not a mount point
  on macOS (and /home need not be one on Linux), so the mount guard did
  not catch it, and remove_dir_all would empty the home directory before
  failing on the root-owned parent.
- A parent of a system tree is refused (/var holds /var/lib; /opt holds
  /opt/homebrew on macOS).
- Before each removal, the worker checks the mount table for a
  filesystem mounted inside the target; remove_dir_all does not stop at
  mount points.
- Guards compare without case on macOS, where the default APFS volume is
  case-insensitive.
- Mounts get their second spelling through /usr/share/firmlinks, so a
  share at /Volumes/NAS is also left out at
  /System/Volumes/Data/Volumes/NAS when a scan starts on the Data volume.
- The macOS system-tree list is compiled only on macOS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant